﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Notes
{
    class NoteDataBase
    {
        private SQLiteConnection m_dbConnection;
       
        //Constructor
        public NoteDataBase()
        {
            Initialize();
        }

        //Initialize values
        private void Initialize()
        {
            m_dbConnection = new SQLiteConnection(@"Data Source=d:\notes.db");
        }

        public List<Note> QueryAllNote()
        {
            string sql = "select * from note";
            
            List<Note> list = new List<Note>();

            m_dbConnection.Open();
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Note note = new Note();

                if (!reader.IsDBNull(6))
                {
                    note.ID = reader.GetInt64(6);
                }

                if (!reader.IsDBNull(8))
                {
                    note.Title = reader.GetString(8);
                }

                if (!reader.IsDBNull(3))
                {
                    note.Creation = reader.GetDateTime(3);
                }

                if (!reader.IsDBNull(5))
                {
                    note.Modified = reader.GetDateTime(5);
                }

                if (!reader.IsDBNull(1))
                {
                    note.Tags = reader.GetString(1);
                }

                if (!reader.IsDBNull(4))
                {
                    note.Flags = reader.GetInt32(4);
                }

                if (!reader.IsDBNull(2))
                {
                    note.Attachments = reader.GetString(2);
                }

                if (!reader.IsDBNull(0))
                {
                    note.Text = reader.GetString(0);
                }
            
                list.Add(note);
           
            }

            m_dbConnection.Close();

            return list;
        }

        private string SQLValue(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return "null";
            }
            else
            {
                return "'" + text + "'";
            }
        }

        public void Insert(Note note)
        {
            string sql = string.Format(@"insert into note (formatxml, tags, attachment, creation, modified, flags, text, title) 
                                           values ({0},{1},datetime('now'),datetime('now'),{2},{3},{4},{5})",
                                                                                                        SQLValue(note.Text),
                                                                                                        SQLValue(note.Tags),
                                                                                                        SQLValue(note.Attachments),
                                                                                                        note.Flags,
                                                                                                        SQLValue(note.Text),
                                                                                                        SQLValue(note.Title));
            m_dbConnection.Open();

            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            m_dbConnection.Close();
        
        }
    }
}
